home *** CD-ROM | disk | FTP | other *** search
/ The X-Philes (2nd Revision) / The X-Philes Number 1 (1995).iso / xphiles / hp48_2 / chip-48 < prev    next >
Text File  |  1995-03-31  |  13KB  |  284 lines

  1. Article 1361 of comp.sys.handhelds:
  2. From: gson@niksula.hut.fi (Andreas Gustafsson)
  3. Newsgroups: comp.sys.handhelds
  4. Subject: CHIP48 version 2.25 (fixes display offset bug)
  5. Summary: Display offset bug fixed
  6. Message-ID: <1990Sep18.104531.584@santra.uucp>
  7. Date: 18 Sep 90 10:45:31 GMT
  8. Organization: Helsinki University of Technology, Finland
  9.  
  10. Here is CHIP48 version 2.25.  It is mostly the same as version 2.20,
  11. except that it is supposed to fix the dreaded "display offset bug".
  12.  
  13. There is one other small difference: the 2.25 Code object is no longer
  14. self-modifying, so you can put this in ROM if you like :-).  However,
  15. that also means that it will no longer remember the previous state of
  16. the sound on/off flag like 2.20 did.  
  17.  
  18. The source and uuencoded binary for version 2.25 will also be made
  19. available for FTP and maybe posted to c.s.h.  Unfortunately they will
  20. be delayed by a week or two because one of the two 40 MB hard disks in
  21. my home machine just broke down, so the machine won't boot although
  22. the source is still safe on the other disk (anybody want to sell or
  23. donate a hard disk? :-()
  24.  
  25.  
  26. For those who don't remember, the CHIP-8 programming language was used
  27. in a number of home computers based on RCA's CDP1802 processor in the
  28. late 1970's.  It's a small, interpreted language designed specifically
  29. for writing simple video games.  It has less than 40 instructions,
  30. including arithmetic, control flow, graphics, and sound.  The
  31. instructions are all 16 bits long and are executed by a very compact
  32. virtual machine interpreter (the 1802 implementation was 512 bytes long).
  33. A simple Pong-style game can usually be written in about 256 bytes of
  34. CHIP-8 and would usually be written directly in hexadecimal.
  35.  
  36. This posting includes documentation on CHIP-8 (terse but reasonably
  37. complete), CHIP-48 itself, and a sample game.  Write more and post
  38. them to comp.sys.handhelds!
  39.  
  40. Make sure you have your calculator backed up before you try running
  41. CHIP-48.  The interpreter was written for a HP48SX with the Revision A
  42. ROM.  It appears that it does _not_ work with other ROM revisions.
  43. Hopefully users with newer ROMs will make the necessary changes and
  44. mail them to me.
  45.  
  46.  
  47. * Technical specification
  48.  
  49. The CHIP-8 virtual machine is byte-addressable and has an address
  50. space of 4 kB at addresses 000-FFF hex.  However, addresses 000-1FF
  51. are reserved (this is where the CHIP-8 interpreter used to reside).
  52. Therefore, the CHIP-8 program itself begins at address 200.  All 
  53. instructions are 16 bits long and by convention instructions always
  54. start at an even address.
  55.  
  56. The machine has 16 8-bit general-purpose registers called V0..VF.  The
  57. VF register is modified by certain instructions and works as a carry
  58. flag and sprite collision indicator.  There is also a 16-bit pointer
  59. register I (though only the low 12 bits are generally used).
  60.  
  61. A CHIP-8 program displays graphics by drawing sprites that are 8
  62. pixels wide and 1..15 pixels high.  The screen resolution is 32 by 64
  63. pixels.  The origin is the upper left corner of the screen, and all
  64. coordinates are positive.  The sprites are stored in big-endian
  65. format, i.e., the most significant bit corresponds to the leftmost
  66. pixel.  All drawing is done in XOR mode.  If this causes one or
  67. more pixels to be erased, VF is set to 01, otherwise 00.
  68.  
  69. There are two timers: the delay timer and the sound timer.  Both
  70. timers count down about 60 times per second when nonzero; the speaker
  71. will beep whenever the sound timer is nonzero.
  72.  
  73. In the instruction table below, NNN is a 12-bit address, KK is an
  74. 8-bit constant, and X and Y are 4-bit register numbers.  Hex
  75. characters represent themselves.  The two first characters of the
  76. instruction code form the lower-address byte of the instruction, the
  77. first character being the more significant nibble.
  78.  
  79. 0NNN    Call 1802 machine code subroutine at NNN
  80. 00E0    Clear display
  81. 00EE    Return from subroutine
  82. 1NNN    Jump to NNN
  83. 2NNN    Call subroutine at NNN
  84. 3XKK    Skip next instruction if VX == KK
  85. 4XKK    Skip next instruction if VX != KK
  86. 5XY0    Skip next instruction if VX == VY
  87. 6XKK    VX := KK
  88. 7XKK    VX := VX + KK
  89. 8XY0    VX := VY, VF may change
  90. 8XY1    VX := VX or VY, VF may change
  91. 8XY2    VX := VX and VY, VF may change
  92. 8XY3    VX := VX xor VY, VF may change *
  93. 8XY4    VX := VX + VY, VF := carry
  94. 8XY5    VX := VX - VY, VF := not borrow
  95. 8XY6    VX := VX shr 1, VF := carry
  96. 8XY7    VX := VY - VX, VF := not borrow *
  97. 8XYE    VX := VX shl 1, VF := carry
  98. 9XY0    Skip next instruction if VX != VY
  99. ANNN    I := NNN
  100. BNNN    Jump to NNN+V0
  101. CXKK    VX := pseudorandom_number and KK
  102. DXYN    Show N-byte sprite from M(I) at coords (VX,VY), VF := collision
  103. EX9E    Skip next instruction if key VX pressed
  104. EXA1    Skip next instruction if key VX not pressed
  105. FX07    VX := delay_timer
  106. FX0A    wait for keypress, store hex value of key in VX
  107. FX15    delay_timer := VX
  108. FX18    sound_timer := VX
  109. FX1E    I := I + VX
  110. FX29    Point I to 5-byte font sprite for hex character VX
  111. FX33    Store BCD representation of VX in M(I)..M(I+2)
  112. FX55    Store V0..VX in memory starting at M(I)
  113. FX65    Read V0..VX from memory starting at M(I)
  114.  
  115. The instructions marked with * may have been undocumented (but
  116. functional) in the original CHIP-8 interpreter for the 1802.
  117.  
  118.  
  119. * Notes on the HP48SX implementation 
  120.  
  121. CHIP-8 programs are stored in the HP48SX as string objects containing
  122. the bytes of the program in increasing address order, beginning with
  123. the byte at 0200.  The interpreter itself is a machine code object
  124. that should be run with the CHIP-8 program string on level 1.  4 kB of
  125. free memory is needed.  If an error is detected during execution, the
  126. address of the current CHIP-8 instruction is returned as a binary
  127. integer on level 1.
  128.  
  129. The clock display should be turned off.  Otherwise, CHIP48 will not run
  130. but will clear flag -40 so that the next attempt to run it may succeed.
  131.  
  132. To quit, press the backspace key.  Pressing ENTER restarts the CHIP-8
  133. program, and the +/- key turns the sound off or on.
  134.  
  135. The 0NNN instruction is unimplemented (of course), except for 
  136. NNN==0E0 or 0EE.  Subroutine nesting is limited to 16 levels.
  137.  
  138. Most chip-8 programs are written for a 16-key hex keyboard with 
  139. following layout:
  140.  
  141.   1 2 3 C                                               7 8 9 /
  142.   4 5 6 D    This keyboard is emulated on the HP48SX    4 5 6 *
  143.   7 8 9 E    using the following keys:                  1 2 3 -
  144.   A 0 B F                                               0 . _ +
  145.  
  146. This may cause some confusion with programs requiring numerical input.
  147. The keys 2, 4, 6, and 8 are commonly used to represent directions.
  148. The F key (+ on the HP48SX) is sometimes used as a Fire button.
  149.  
  150.  
  151. * The interpreter
  152.  
  153. Below is a HP48SX machine code object that should be uudecoded and
  154. then downloaded to the calculator using Kermit.
  155.  
  156.  
  157. Decode this using Bill Wickes' ASC-> program and save it as CHIP.  The
  158. checksum should be #9D0Eh and the length as calculated using
  159. "blue-arrow CHIP BYTES" should be 1738 bytes (slightly shorter that
  160. version 2.20).
  161.  
  162. %%HP: T(1)A(D)F(.);
  163. "CCD20F8D008FFBA818FB976034EC607134152080863F08084315006A9184A345
  164. C1008FD7B5013681AF0C134AF2154716F154716F370000100415C781AF143417
  165. 000C213434B550780821837BC13415507808218079B184A34000208FD7B50136
  166. 81AF0AAF0AF1AF2AF381AF123400400C213414713514334C2A208A6A11741438
  167. 18F843400C108B2606B311747D718F2D760174E78FB976081AF1A13481B43409
  168. B00C2135808210515F0F217015C1161CC96CBE808FD281AF083200281AF0B81A
  169. F1434C2000C2134D21447E53D232831134146DA1468A68F8082408F000EF281A
  170. F017DE05A180874B98F2D760142164808C808F8F75D0081AF1BDA818F0181AF0
  171. 377C1135D217015701C0C6C681B4CA3483B00C2134D215E381B4CA81B5818F2B
  172. 0681B2460697F11B1088F735608FB97606F6F135147818F2F818F23144818F2F
  173. 818F2F818F21164A6C96C2E018A86115F015C0160170CC6AEF01715114E96AB2
  174. 161156090AF116015E2801BBE8082200C0E3215C2D232831134146DA1468A68F
  175. 8082408F000EF281AF118A6606F70D23108EA3408F000EF681AF0175C014E96A
  176. 80A6E14C16114E96A80A6E14C70C0320108018F06110DAD280115E31583FC0EF
  177. 68087400808700080873703010374701560FE8082010EF2154062EFD22215702
  178. 017114F1C101C681AF12C201D21570C681AF14C213401D23006BEFD230F62EFD
  179. 2310168DFD231116ECFD2312164CFD231416ABF7DAFD214E061727F9FD014A07
  180. 1C2011727C8F1C2D215E1067D7FD015A107DE01F6F680821100E62067A7F0714
  181. C03C681B4CA34BA800C2134D215E08018F06110DAD280116015E00EF20113613
  182. 406762F81AF187BFE13507DA0179DEDAD2310E8A2D031EE8A2640281AF143417
  183. 000CA3104D5130146134AF2154716F154716F14C818F04A6D96D7D0381AF1434
  184. C2000C21341428A800818F83140818F24C2134D215E381AF0B037F3E81AF0B03
  185. 81AF1434C2000C2134142818F24C213681AF131583136142818F0334040008B6
  186. 001406EAF7B0E17114B14E9664181AF2B818F2181AF2B0375ED17114B14E962E
  187. E69DF7E1E6CCF761E6AEF7FBD17114B1480370BD17114B14EA6214C0317115B0
  188. 1C1300906B076FD148033019060175ED0E6A15C103302906017FCD0E6215C103
  189. 303906D179BDAE80E690E62BEE0E6514C0330490601769DC215C161BD3059065
  190. 1708DBECC2E615C1669D30690691756DAEA8196215C1AE66B7D307906C0764DD
  191. E63CF30E90601743DC615C16F4D02769C81AF08037BAC768CD014AC281AF0B03
  192. 749C344010013615A113617114F1C10E6214C036561754B47F90A6F70E281AF1
  193. 8774C81AF08789CD731F10E6631F30E63171AE215F0A62AE53102B61550AE1AE
  194. 215F0B6980D080CF2EA82201C175037DB28EE0CFAE22E90A50B6614C200378DB
  195. D214E7A7CAE3A8717114B1C131E9962D0311A962E002AEBAE069CDAEBAE0699D
  196. 13606D3DB763C90E017E5A413B076AEF75AB314015C1744A471DB7A0C90E4E07
  197. 134AEB030702784B17114B1C131709668113606745B14A071361480331A09664
  198. 17B7F47014C03076AF831519662114E067A1B0714C0331819662114E067B0B07
  199. 14C0331E1966B1D214E81AF10A3A40081AF0003319296681D215E0DAC6C6C281
  200. AF0803313396260639014EAE781B434FB300C2134D096B42AEB808A0C015E205
  201. CA04162819636EDF02D881AF1880824DFF008B2007F1A134D4D2221500160154
  202. 016021150016015401602015001601540033155966537BCA14E14D1361348BAE
  203. 117116181AF18B3640081AF0866DF033156966337F8A14F14C1361348BA2E171
  204. 16181AF18B3640081AF0866DF0281AF14341B100C213481AF1814416481AF191
  205. 4416481AF1A14416481AF1B1441640181AF14341B100C213414681AF08164146
  206. 81AF0916414681AF0A16414681AF0B164012C300202C90E40C420D6C4C4C281A
  207. F14CA3417000C281AF09DB819F2819F2819F2C6C681AF0A31700E63C781B434C
  208. 3000C2CB81AF0B81AF1813415E1D1AE5136818F2181AF0881AF1B81B3C5C5C5C
  209. 5C5C5C5C581B434F1100CAD2A89C6C213414EAE7BF3BF3F5D2A89C6C213414EA
  210. E7BF3BF3F5D2A89C6C213414EAE7BF3BF3F5D2A89C6C213414EAE781AF111301
  211. 462C90E80818F04818F042081AF0181AF12C21343102B6280821809E650AE6A6
  212. E80D01561A9A0E1791AF080FD2E30180FDA9F0E1AB9F0E1780FD2C90EB180FD1
  213. 54116F16F16115416B0080FD154120A4E94A6067AE01000C030FC0CCC3CF303C
  214. 333FF0FCF3FFF999F26227F1F8FF1F1F99F11F8F1FF8F9FF1244F9F9FF9F1FF9
  215. F99E9E9EF888FE999EF8F8FF8F88100200400800610230460821148884824844
  216. 422824221812814121116A200430C4305930BB301D301E300F3050409D309E40
  217. 5F40C05073508C508560E0D9"
  218. -- 
  219. Andreas Gustafsson
  220. Internet: gson@niksula.hut.fi
  221. Voice: +358 0 563 5592
  222.  
  223.  
  224.  
  225. * Sample game
  226.  
  227. Not particularly exciting, but I had to include something to show how
  228. the interpreter is used.  Uuencode, download to HP48SX using Kermit,
  229. and run with BRIX CHIP.  Play by pressing the keys 4 and 6.
  230.  
  231. begin 644 brix
  232. M2%!(4#0X+4$L*E C &X%90!K!FH HPS:L7H$.D 2"'L".Q(2!FP@;1^C$-S1
  233. M(O9@ &$ HQ+0$7 (HP[0$6! \!7P!S  $C3&#V<>: %I_Z,.UG&C$-S18 3@
  234. MH7S^8 ;@H7P"8#^, MS1HP[6<8:$AY1@/X8"81^'$D<?$JQ& &@!1C]H_T< 
  235. M:0'6<3\!$JI''Q*J8 6 =3\ $JI@ ? 8@&!A_( 2HPS0<6#^B0,B]G4!(O9%
  236. M8!+>$D9I_X!@@,4_ 1+*80* %3\!$N" %3\!$NZ %3\!$NA@(/ 8HPY^_X#@
  237. M@ 1A - 1/@ 2,!+>>/](_FC_$NYX 4@": %@!/ 8:?\2<*,4]3/R9?$I8S=D
  238. 7 --%<P7R*=-% .[@ (  _ "J       "
  239.  
  240. end
  241.  
  242.  
  243. * Source code availability
  244.  
  245. The source code for the interpreter is over 30 k of assembler, and to
  246. assemble it you need my ASAP assembler which is about 25 k.  To run
  247. the assembler, you need Perl 3.0 which is several hundred k.
  248. Therefore, I am not posting all these to comp.sys.handhelds.  However,
  249. the source to CHIP48 and ASAP are available by anonymous FTP from
  250. vega.hut.fi, directory /pub/misc/hp48sx/asap.  The source to Perl 3.0
  251. is available from numerous Unix archive sites.
  252.  
  253. Those who want to write their own machine code games may want to look
  254. at the interpreter source for hints.  Please note that I have no
  255. insider technical information on the HP48SX; CHIP-8 is based solely on
  256. information posted to comp.sys.handhelds (in particular, Alonzo
  257. Gariepy's excellent HP28S Processor Notes) and personal HP48SX machine
  258. code hacking.
  259.  
  260.  
  261. * Fine print
  262.  
  263.       CHIP-48 and BRIX are (C) Copyright 1990 Andreas Gustafsson
  264.  
  265.       Noncommercial distribution allowed, provided that this
  266.       copyright message is preserved, and any modified versions
  267.       are clearly marked as such.  
  268.  
  269.       CHIP-48 and BRIX make use of undocumented low-level features of
  270.       the HP48SX calculator, and may or may not cause loss of data,
  271.       excessive battery drainage, and/or damage to the calculator
  272.       hardware.  The Author takes no responsibility whatsoever for 
  273.       any damage caused by the use of this program.
  274.  
  275.       THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
  276.       IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  277.       WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
  278.       PURPOSE.
  279.  
  280. -- 
  281. Andreas Gustafsson
  282. Internet: gson@niksula.hut.fi
  283. Voice: +358 0 563 5592
  284.